<?php

// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 订单结算ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   create by allen.qiang
 */
quanli_require('module/base/callcenter/callcenterWorkOrder.class.php');

class ajax_callcenterWorkOrder {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && is_numeric($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch ($_REQUEST['operFlg']) {

                case 1:
                    $this->workOrderList();
                    break;

                case 100:
                    $this->saveWorkOrder();//配送对账
                    break;

            }
        } else {

            $this->toReturn('参数错误');
        }
    }

    /**
     * 返回信息
     * @param string $message
     * @param int $code
     * @param mixed $data
     */
    private function toReturn($message, $code = 400, $data = '') {

        die(json_encode(array('code' => $code, 'message' => $message, 'data' => $data)));
    }

    /**
     * 查询订单信息
     */
    private function workOrderList() {

        global $G_SHOP;

        $result_array = array();

        if(isset($_REQUEST['pcustID']) && !empty($_REQUEST['pcustID'])){

            if (!isset($_REQUEST['page']) || !quanli_is_int($_REQUEST['page'])) {
                $_REQUEST['page'] = 1;
            }


            //实例化工单类
            $callcenterWorkOrder = new callcenterWorkOrder($G_SHOP->DBCA);

            $_REQUEST['sortname'] = 'createTime';
            $_REQUEST['sortorder'] = 'DESC';
            $_REQUEST['rp'] = 10;
            $_REQUEST['sortorder'] = 'DESC';
            $_REQUEST['sortorder'] = 'DESC';

            $return_array['work'] = $callcenterWorkOrder->searchWorkOrder($_REQUEST);

            if($return_array['work']){
                $return_array['paging'] = shop_page($_REQUEST['page'], $_REQUEST['count'], $_REQUEST['rp'], 'javascript: workOrder.search(%d);', $G_SHOP->smarty , $tpl = 'comm/page.tpl');
            }

            $result_array['code'] = 200;
            $result_array['data'] = $return_array;

            echo json_encode($result_array);
        }else{

            $result_array['code'] = 200;
            $result_array['data'] = array();

            echo json_encode($result_array);
        }


    }

    public function saveWorkOrder(){

        global $G_SHOP;

        //实例化工单类
        $callcenterWorkOrder = new callcenterWorkOrder($G_SHOP->DBCA);

        $_REQUEST['creator'] =$G_SHOP->currentUserID;

        $return_array = $callcenterWorkOrder->saveCallcenterWorkOrder($_REQUEST);

        echo json_encode($return_array);

    }

}
